package com.shsoft.utils;

import com.shsoft.colorfulcc.MainActivity;
import com.shsoft.common.Constant;
import com.shsoft.entity.CompositeItem;

import org.litepal.LitePal;

import java.util.List;

/**
 * 解析数据的类
 */
public class DataUtil {

    // 将一段数据解析为灯珠信息并将移动端的灯珠显示为数据指定颜色
    public static void doData(byte[] data, MainActivity activity){


    }


    // 将配置好的灯珠显示信息解析成字节数组
    public static byte[] getData(MainActivity activity){
        StringBuilder sb = new StringBuilder();
        sb.append("FF"); // 起始码
        if (activity.colorMode == Constant.COLOR_MODE_SINGLE){  // 纯色模式，20组颜色就是预设区的颜色,如果预设颜色只有3个，那就填充3个
            // 1-60字节，颜色
            for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                if(i < activity.colors.size()){
                    int[] rgb = activity.colors.get(i);
                    sb.append(HexUtil.intStrToHexStr(rgb[0] + ""));
                    sb.append(HexUtil.intStrToHexStr(rgb[1] + ""));
                    sb.append(HexUtil.intStrToHexStr(rgb[2] + ""));
                }else { // 没有预设颜色，填充00
                    sb.append("00");
                    sb.append("00");
                    sb.append("00");
                }
            }
            // 61字节，灯珠的显示模式（1.常亮  2.跑马  3.流水  4.呼吸  5.复杂模式）
            sb.append(HexUtil.intStrToHexStr(activity.lightMode + ""));
            // 62字节，颜色显示模式（1-20：多少种纯色预设 250：混色）
            sb.append(HexUtil.intStrToHexStr(activity.colors.size() + ""));
            // 以下字节分模式
            if(activity.lightMode == Constant.LIGHT_MODE_LONG){
                sb.append(HexUtil.intStrToHexStr(activity.long_bright.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.long_interval.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.long_offtime.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.long_circle.getText().toString()));
                sb.append("FFFF");
            }else if(activity.lightMode == Constant.LIGHT_MODE_HORSE){
                sb.append(HexUtil.intStrToHexStr(activity.horse_bright.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.horse_direction_value + ""));
                sb.append(HexUtil.intStrToHexStr(activity.horse_speed.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.horse_circle.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.horse_count.getText().toString()));
                sb.append("FFFF");
            }else if(activity.lightMode == Constant.LIGHT_MODE_FLOWING_WATER){
                sb.append(HexUtil.intStrToHexStr(activity.flowingWater_bright.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.flowingWater_direction_value + ""));
                sb.append(HexUtil.intStrToHexStr(activity.flowingWater_speed.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.flowingWater_circle.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.flowingWaterMode + ""));
                sb.append("FFFF");
            }else if(activity.lightMode == Constant.LIGHT_MODE_BREATHING){
                sb.append(HexUtil.intStrToHexStr(activity.breathing_bright.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.breathing_time.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.breathing_circle.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.breathingMode + ""));
                sb.append("FFFF");
            }else if(activity.lightMode == Constant.LIGHT_MODE_COMPOSITE){
                // 查询出所有的步骤
                List<CompositeItem> compositeList = LitePal.where("colorMode=?",activity.colorMode + "").order("id asc").find(CompositeItem.class);
                for (CompositeItem item : compositeList) {
                    // 获取item里的颜色编号
                    String[] colors = item.getColorNum().split(",");
                    // 亮灯模式
                    sb.append(HexUtil.intStrToHexStr(item.getLightMode() + ""));
                    if(item.getLightMode() == Constant.LIGHT_MODE_LONG){
                        sb.append(HexUtil.intStrToHexStr(item.getArg1() + "")); // 亮度
                        sb.append(HexUtil.intStrToHexStr(item.getArg2() + "")); // 亮灯时间
                        sb.append(HexUtil.intStrToHexStr(item.getArg3() + "")); // 灭灯时间
                        sb.append(HexUtil.intStrToHexStr(item.getArg4() + "")); // 轮数
                    }else if(item.getLightMode() == Constant.LIGHT_MODE_HORSE){
                        sb.append(HexUtil.intStrToHexStr(item.getArg1() + "")); // 亮度
                        sb.append(HexUtil.intStrToHexStr(item.getArg2() + "")); // 方向
                        sb.append(HexUtil.intStrToHexStr(item.getArg3() + "")); // 速度
                        sb.append(HexUtil.intStrToHexStr(item.getArg4() + "")); // 圈数
                        sb.append(HexUtil.intStrToHexStr(item.getArg5() + "")); // 灯数
                    }else if(item.getLightMode() == Constant.LIGHT_MODE_FLOWING_WATER){
                        sb.append(HexUtil.intStrToHexStr(item.getArg1() + "")); // 亮度
                        sb.append(HexUtil.intStrToHexStr(item.getArg2() + "")); // 方向
                        sb.append(HexUtil.intStrToHexStr(item.getArg3() + "")); // 速度
                        sb.append(HexUtil.intStrToHexStr(item.getArg4() + "")); // 圈数
                        sb.append(HexUtil.intStrToHexStr(item.getArg5() + "")); // 流水模式
                    }else if(item.getLightMode() == Constant.LIGHT_MODE_BREATHING){
                        sb.append(HexUtil.intStrToHexStr(item.getArg1() + "")); // 最大亮度
                        sb.append(HexUtil.intStrToHexStr(item.getArg2() + "")); // 呼吸时间
                        sb.append(HexUtil.intStrToHexStr(item.getArg3() + "")); // 呼吸次数
                        sb.append(HexUtil.intStrToHexStr(item.getArg4() + "")); // 呼吸模式
                    }
                    // 循环添加颜色编号
                    for (int i = 0; i < colors.length; i++) {
                        String colorNum = colors[i].replace("#","");
                        sb.append(HexUtil.intStrToHexStr(colorNum));
                    }
                    // 添加步骤结尾
                    sb.append("FF");
                }
                sb.append("FFFF");
            }
        }else { // 混色模式
            // 1-60字节，颜色
            if(activity.lightMode == Constant.LIGHT_MODE_COMPOSITE){ // 复合模式，20个颜色就是20个预设颜色
                for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                    if(i < activity.colors.size()){
                        int[] rgb = activity.colors.get(i);
                        sb.append(HexUtil.intStrToHexStr(rgb[0] + ""));
                        sb.append(HexUtil.intStrToHexStr(rgb[1] + ""));
                        sb.append(HexUtil.intStrToHexStr(rgb[2] + ""));
                    }else { // 没有预设颜色，填充00
                        sb.append("00");
                        sb.append("00");
                        sb.append("00");
                    }
                }
            }else { // 非复合模式，20个颜色就是20个灯珠的颜色
                for (int i = 0; i < Constant.LIGHT_COUNT; i++){
                    int colorIndex = activity.lights.get(i);
                    if(colorIndex == 20 || colorIndex >= activity.colors.size()){ // 无颜色，填充0
                        sb.append("00");
                        sb.append("00");
                        sb.append("00");
                    }else {
                        sb.append(HexUtil.intStrToHexStr(activity.colors.get(colorIndex)[0] + ""));
                        sb.append(HexUtil.intStrToHexStr(activity.colors.get(colorIndex)[1] + ""));
                        sb.append(HexUtil.intStrToHexStr(activity.colors.get(colorIndex)[2] + ""));
                    }
                }
            }
            // 61字节，灯珠的显示模式（1.常亮  2.跑马  3.流水  4.呼吸  5.复杂模式）
            sb.append(HexUtil.intStrToHexStr(activity.lightMode + ""));
            // 62字节，颜色显示模式（1-20：多少种纯色预设 250：混色）
            sb.append(HexUtil.intStrToHexStr(250 + ""));
            // 以下字节分模式
            if(activity.lightMode == Constant.LIGHT_MODE_LONG){
                sb.append(HexUtil.intStrToHexStr(activity.long_bright.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.long_interval.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.long_offtime.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.long_circle.getText().toString()));
                sb.append("FFFF");
            }else if(activity.lightMode == Constant.LIGHT_MODE_HORSE){
                sb.append(HexUtil.intStrToHexStr(activity.horse_bright.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.horse_direction_value + ""));
                sb.append(HexUtil.intStrToHexStr(activity.horse_speed.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.horse_circle.getText().toString()));
                sb.append("FFFF");
            }else if(activity.lightMode == Constant.LIGHT_MODE_FLOWING_WATER){
                sb.append(HexUtil.intStrToHexStr(activity.flowingWater_bright.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.flowingWater_direction_value + ""));
                sb.append(HexUtil.intStrToHexStr(activity.flowingWater_speed.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.flowingWater_circle.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.flowingWaterMode + ""));
                sb.append("FFFF");
            }else if(activity.lightMode == Constant.LIGHT_MODE_BREATHING){
                sb.append(HexUtil.intStrToHexStr(activity.breathing_bright.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.breathing_time.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.breathing_circle.getText().toString()));
                sb.append(HexUtil.intStrToHexStr(activity.breathingMode + ""));
                sb.append("FFFF");
            }else if(activity.lightMode == Constant.LIGHT_MODE_COMPOSITE){
                // 查询出所有的步骤
                List<CompositeItem> compositeList = LitePal.where("colorMode=?",activity.colorMode + "").order("id asc").find(CompositeItem.class);
                for (CompositeItem item : compositeList){
                    // 获取item里的颜色编号
                    String[] colors = item.getColorNum().split(",");
                    // 亮灯模式
                    sb.append(HexUtil.intStrToHexStr(item.getLightMode() + ""));
                    if(item.getLightMode() == Constant.LIGHT_MODE_LONG){
                        sb.append(HexUtil.intStrToHexStr(item.getArg1() + "")); // 亮度
                        sb.append(HexUtil.intStrToHexStr(item.getArg2() + "")); // 亮灯时间
                        sb.append(HexUtil.intStrToHexStr(item.getArg3() + "")); // 灭灯时间
                        sb.append(HexUtil.intStrToHexStr(item.getArg4() + "")); // 轮数
                    }else if(item.getLightMode() == Constant.LIGHT_MODE_HORSE){
                        sb.append(HexUtil.intStrToHexStr(item.getArg1() + "")); // 亮度
                        sb.append(HexUtil.intStrToHexStr(item.getArg2() + "")); // 方向
                        sb.append(HexUtil.intStrToHexStr(item.getArg3() + "")); // 速度
                        sb.append(HexUtil.intStrToHexStr(item.getArg4() + "")); // 圈数
                    }else if(item.getLightMode() == Constant.LIGHT_MODE_FLOWING_WATER){
                        sb.append(HexUtil.intStrToHexStr(item.getArg1() + "")); // 亮度
                        sb.append(HexUtil.intStrToHexStr(item.getArg2() + "")); // 方向
                        sb.append(HexUtil.intStrToHexStr(item.getArg3() + "")); // 速度
                        sb.append(HexUtil.intStrToHexStr(item.getArg4() + "")); // 圈数
                        sb.append(HexUtil.intStrToHexStr(item.getArg5() + "")); // 流水模式
                    }else if(item.getLightMode() == Constant.LIGHT_MODE_BREATHING){
                        sb.append(HexUtil.intStrToHexStr(item.getArg1() + "")); // 最大亮度
                        sb.append(HexUtil.intStrToHexStr(item.getArg2() + "")); // 呼吸时间
                        sb.append(HexUtil.intStrToHexStr(item.getArg3() + "")); // 呼吸次数
                        sb.append(HexUtil.intStrToHexStr(item.getArg4() + "")); // 呼吸模式
                    }
                    // 添加颜色编号
                    for (int i = 0; i < colors.length; i++) {
                        String colorNum = colors[i].replace("#","");
                        sb.append(HexUtil.intStrToHexStr(colorNum));
                    }
                }
                sb.append("FFFF");
            }
        }
        return sb.toString().getBytes();
    }
}
